package com.vision.sales.sv.report;

import java.util.HashMap;
import java.util.Map;

import com.vision.sales.cm.data.Month;
import com.vision.sales.cm.db.data.Salesman;

/**
 * 
 * @author Mark
 *
 */
public class SalesmanSalesRow implements Comparable<SalesmanSalesRow> {
	
	private Salesman salesman;
	private Map<String, Double> salesMap;
	
	public SalesmanSalesRow(Salesman salesman, Map<String, Double> salesMap) {
		this.salesman = salesman;
		this.salesMap = salesMap;
	}

	public Object get(String column) {
		if ("salesman".equals(column))
			return salesman.getName();
		
		return salesMap.get(column);
	}
	
	public Salesman getSalesman() {
		return salesman;
	}
	
	public Map<String, Double> getSalesMap() {
		if (salesMap == null)
			salesMap = new HashMap<String, Double>();
		return salesMap;
	}
	
	public void computeTotal() {
		double total = 0;
		for (String month : Month.MONTHS) {
			Double value = getSalesMap().get(month);
			if (value != null)
				total += value;
			else
				getSalesMap().put(month, 0d);
		}
		
		getSalesMap().put("total", total);
	}
	
	@Override
	public int compareTo(SalesmanSalesRow r) {
		return getSalesman().getName().compareToIgnoreCase(r.getSalesman().getName());
	}

}
